/**
 * 商品
 */
angular.module('mall.goods.base', [])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider

        //商品列表页
            .state('goods', {
                url: '/shop/:shopid',
                'abstract': true,
                template: '<ui-view/>',
                resolve: {
                    shopinfo: ['$stateParams', 'ShopService', function ($stateParams, ShopService) {
                        if ($stateParams.name) {
                            return {
                                shopid: $stateParams.shopid,
                                name: $stateParams.name
                            };
                        }
                        var shopinfo = ShopService.queryShopByShopid($stateParams.shopid);
                        if (shopinfo.$promise) {
                            return shopinfo.$promise;
                        }
                        return shopinfo;
                    }],
                    menulist: ['$stateParams', 'ShopMenuService', function ($stateParams, ShopMenuService) {
                        var menulist = ShopMenuService.getMenuByShopid($stateParams.shopid);
                        return menulist;
                    }]
                },
                controller: 'GoodsBaseController'
            })
    }])

    //商品列表控制器
    .controller('GoodsBaseController', ['$scope','$state', '$stateParams', 'ShopMenuService', 'ShopcartService', 'shopinfo', 'menulist',
        function ($scope,$state, $stateParams, ShopMenuService, ShopcartService, shopinfo, menulist) {
            $scope.shopid = $stateParams.shopid;
            $scope.shopname = shopinfo.name;
            $scope.goods_list = menulist;
            $scope.goodscontent = ShopMenuService.gettotalpriceandcount($scope.shopid);
            setOrderNumbers($scope.goodscontent);

            $scope.go_back = function () {
                window.history.go(-1);
            }
            //show_hide("#hidval", ".background_hide", "#demo");//背景滚动设置
            $scope.add = function (cid, goodsid) {//增加按钮
                ShopMenuService.addGood($scope.shopid, cid, goodsid);
                $scope.goodscontent = ShopMenuService.gettotalpriceandcount($scope.shopid);
                setOrderNumbers($scope.goodscontent);
            }

            $scope.remove = function (cid, goodsid) {//删除按钮
                ShopMenuService.removeGood($scope.shopid, cid, goodsid);
                $scope.shopbuygoods = ShopMenuService.getShopCarByShopid($scope.shopid);
                $scope.goodscontent = ShopMenuService.gettotalpriceandcount($scope.shopid);
                setOrderNumbers($scope.goodscontent);
                if ($scope.goodscontent.totalprice == 0&&$("#hidval").val()==1) {
                    show_hide("#hidval", ".background_hide", "#demo");
                }
                $scope.float_more();
            }
            $scope.sure_clean = function () {
                $(".contain-item").fadeIn();
                $scope.cleaning = function () {//清空
                    ShopMenuService.removeShopById($scope.shopid);
                    $scope.shopbuygoods = ShopMenuService.getShopCarByShopid($scope.shopid);
                    $scope.goodscontent = ShopMenuService.gettotalpriceandcount($scope.shopid);
                    $scope.float_more();
                    show_hide("#hidval", ".background_hide", "#demo");
                    $(".contain-item").fadeOut();
                }
            }
            $scope.buy_change = function (cid, goodsid, num) {
                ShopMenuService.inputChange($scope.shopid, cid, goodsid,num);
                $scope.goodscontent = ShopMenuService.gettotalpriceandcount($scope.shopid);
                setOrderNumbers($scope.goodscontent);
            }

            $scope.float_show = function () {
                $scope.shopbuygoods = ShopMenuService.getShopCarByShopid($scope.shopid);
                if($scope.shopbuygoods.length==0){
                    input_alert("您清单为空");
                }else {
                    show_hide("#hidval", ".background_hide", "#demo");
                    $scope.float_more();
                }
            }

            $scope.float_more = function () {
                var height = $("#float_more").height();
                var hei = $("#float_more");
                if ($scope.shopbuygoods.length > 6) {
                    hei.addClass("float_more");
                } else {
                    hei.removeClass("float_more");
                }
            }

            $scope.input = function (cid, goodsid, num) {
                ShopMenuService.inputBlur($scope.shopid, cid, goodsid,num);
            }
            //立即购买操作
            $scope.buyNow = function(){
                var shopList = ShopMenuService.buyNowShopById($scope.shopid);
                if(shopList.length==0){
                    input_alert("您清单为空");
                }else{
                    setCookie('placeorder','order',30);
                    $state.go("placeorder", {order: shopList});
                }
            }
            $scope.addToCart = function(){
                $scope.shopbuygoods = ShopMenuService.getShopCarByShopid($scope.shopid);
                if($scope.shopbuygoods.length==0){
                    input_alert("您清单为空");
                }else{
                    ShopMenuService.addToCartShopById($scope.shopid);
                    $("#demo").hide();
                    $("#hidval").val("0");
                    input_alert("加入购物车成功");
                }

            }
            function setOrderNumbers(goodscontent){
                if(goodscontent.count>99){
                    goodscontent.count = "99+";
                }
            }
        }
    ])

;
